package com.syxd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syxd.common.constant.Constant;
import com.syxd.common.entity.WebResult;
import com.syxd.common.exception.ErrorCode;
import com.syxd.common.utils.BeanHelper;
import com.syxd.dao.SysCreditMasterDao;
import com.syxd.dao.SysJiangCreditDao;
import com.syxd.dao.SysSunCreditDao;
import com.syxd.dto.SysCreditDto;
import com.syxd.dto.SysJiangCreditDto;
import com.syxd.dto.SysSunCreditDto;
import com.syxd.entity.SysJiangCreditEntity;
import com.syxd.entity.SysSunCreditEntity;
import com.syxd.service.SysCreditMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;


@Service
@Transactional
public class SysCreditMasterServiceImpl implements SysCreditMasterService {
    @Autowired
    SysCreditMasterDao sysCreditMasterDao;
    @Autowired
    SysSunCreditDao sysSunCreditDao;
    @Autowired
    SysJiangCreditDao sysJiangCreditDao;

    @Override
    public WebResult queryHouseholdQuota(SysCreditDto sysCreditDtoInput) {


        QueryWrapper<SysSunCreditEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(sysCreditDtoInput.getHouseholderIdcard())) {
            queryWrapper.eq("householder_idcard", sysCreditDtoInput.getHouseholderIdcard());
        }
        if (!StringUtils.isEmpty(sysCreditDtoInput.getHouseholderName())) {
            queryWrapper.eq("householder_name", sysCreditDtoInput.getHouseholderName());
        }
        if (!StringUtils.isEmpty(sysCreditDtoInput.getHouseholderPhone())) {
            queryWrapper.eq("householder_phone", sysCreditDtoInput.getHouseholderPhone());
        }
        if (!StringUtils.isEmpty(sysCreditDtoInput.getSpouseIdcard())) {
            queryWrapper.eq("spouse_idcard", sysCreditDtoInput.getSpouseIdcard());
        }
        if (!StringUtils.isEmpty(sysCreditDtoInput.getSpousePhone())) {
            queryWrapper.eq("spousephone", sysCreditDtoInput.getSpousePhone());
        }
        if (!StringUtils.isEmpty(sysCreditDtoInput.getSpouseName())) {
            queryWrapper.eq("spouse_name", sysCreditDtoInput.getSpouseName());
        }

        queryWrapper.eq("is_delete", Constant.FDELETE).last("limit 1");

        List<SysSunCreditEntity> sysSunCreditEntities = sysSunCreditDao.selectList(queryWrapper);
        QueryWrapper<SysJiangCreditEntity> queryWrapper1 = new QueryWrapper<>();
        if (!StringUtils.isEmpty(sysCreditDtoInput.getHouseholderIdcard())) {
            queryWrapper1.eq("householder_idcard", sysCreditDtoInput.getHouseholderIdcard());
        }
        if (!StringUtils.isEmpty(sysCreditDtoInput.getHouseholderName())) {
            queryWrapper1.eq("householder_name", sysCreditDtoInput.getHouseholderName());
        }
        if (!StringUtils.isEmpty(sysCreditDtoInput.getHouseholderPhone())) {
            queryWrapper1.eq("householder_phone", sysCreditDtoInput.getHouseholderPhone());
        }
        if (!StringUtils.isEmpty(sysCreditDtoInput.getSpouseIdcard())) {
            queryWrapper1.eq("spouse_idcard", sysCreditDtoInput.getSpouseIdcard());
        }
        if (!StringUtils.isEmpty(sysCreditDtoInput.getSpousePhone())) {
            queryWrapper1.eq("spousephone", sysCreditDtoInput.getSpousePhone());
        }
        if (!StringUtils.isEmpty(sysCreditDtoInput.getSpouseName())) {
            queryWrapper1.eq("spouse_name", sysCreditDtoInput.getSpouseName());
        }

        queryWrapper1.eq("is_delete", Constant.FDELETE).last("limit 1");

        List<SysJiangCreditEntity> sysJiangCreditEntitieList = sysJiangCreditDao.selectList(queryWrapper1);
        SysCreditDto sysCreditDto = new SysCreditDto();
        if (sysSunCreditEntities.size()>0) {
            sysCreditDto.setSunCreDitQuota(sysSunCreditEntities.get(0).getPublicQuota());
        }
        if (sysJiangCreditEntitieList.size()>0) {
            sysCreditDto.setJiangCreDitQuota(sysJiangCreditEntitieList.get(0).getPublicQuota());
        }
        if (sysCreditDto.getJiangCreDitQuota()==null&&sysCreditDto.getSunCreDitQuota()==null){
            return new WebResult<>().error(ErrorCode.THIS_INFO_NOT_EXIST);
        }
        return new WebResult<SysCreditDto>().ok(sysCreditDto);
    }

    @Override
    public WebResult queryJiangCredit(Map<String, Object> params) {
        QueryWrapper<SysJiangCreditEntity> queryWrapper1 = new QueryWrapper<>();
        if (!StringUtils.isEmpty(params.get("householderName")) ){
            queryWrapper1.eq("householder_name",(String) params.get("householderName"));
        }
        if (!StringUtils.isEmpty(params.get("householderIdcard"))) {
            queryWrapper1.eq("householder_name", (Integer) params.get("householderIdcard"));
        }
        if (!StringUtils.isEmpty(params.get("householderPhone"))) {
            queryWrapper1.eq("householder_phone",(Integer) params.get("householderPhone"));
        }
        if (!StringUtils.isEmpty(params.get("spouseName"))) {
            queryWrapper1.eq("spouse_idcard", (String) params.get("spouseName"));
        }
        if (!StringUtils.isEmpty(params.get("spousePhone"))) {
            queryWrapper1.eq("spousephone", (Integer) params.get("spousePhone"));
        }
        if (!StringUtils.isEmpty(params.get("spouseIdcard"))) {
            queryWrapper1.eq("spouse_name", (Integer) params.get("spouseIdcard"));
        }
        queryWrapper1.eq("is_delete", Constant.FDELETE).last("limit 1");
        List<SysJiangCreditEntity> sysJiangCreditEntitieList = sysJiangCreditDao.selectList(queryWrapper1);
        if (sysJiangCreditEntitieList.size()>0){
            SysJiangCreditDto sysJiangCreditDto = BeanHelper.copyProperties(sysJiangCreditEntitieList.get(0), SysJiangCreditDto.class);
            return new WebResult<SysJiangCreditDto>().ok(sysJiangCreditDto);
        }
        return new WebResult<>().error(ErrorCode.THIS_INFO_NOT_EXIST);
    }


    @Override
    public WebResult querySunCredit(Map<String, Object> params) {
        QueryWrapper<SysSunCreditEntity> queryWrapper1 = new QueryWrapper<>();
        if (!StringUtils.isEmpty(params.get("householderName")) ){
            queryWrapper1.eq("householder_name",(String) params.get("householderName"));
        }
        if (!StringUtils.isEmpty(params.get("householderIdcard"))) {
            queryWrapper1.eq("householder_name", (Integer) params.get("householderIdcard"));
        }
        if (!StringUtils.isEmpty(params.get("householderPhone"))) {
            queryWrapper1.eq("householder_phone",(Integer) params.get("householderPhone"));
        }
        if (!StringUtils.isEmpty(params.get("spouseName"))) {
            queryWrapper1.eq("spouse_idcard", (String) params.get("spouseName"));
        }
        if (!StringUtils.isEmpty(params.get("spousePhone"))) {
            queryWrapper1.eq("spousephone", (Integer) params.get("spousePhone"));
        }
        if (!StringUtils.isEmpty(params.get("spouseIdcard"))) {
            queryWrapper1.eq("spouse_name", (Integer) params.get("spouseIdcard"));
        }
        queryWrapper1.eq("is_delete", Constant.FDELETE).last("limit 1");
        List<SysSunCreditEntity> sysSunCreditEntitieList = sysSunCreditDao.selectList(queryWrapper1);
        if (sysSunCreditEntitieList.size()>0){
            SysSunCreditDto sysSunCreditDto = BeanHelper.copyProperties(sysSunCreditEntitieList.get(0), SysSunCreditDto.class);
            return new WebResult<SysSunCreditDto>().ok(sysSunCreditDto);
        }
        return new WebResult<>().error(ErrorCode.THIS_INFO_NOT_EXIST);
    }



}
